//
//  TTAlertView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/2.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

fileprivate let BUTTON_OKAY = "确认"
fileprivate let BUTTON_CANCEL = "取消"
fileprivate let BUTTON_YES = "是"
fileprivate let BUTTON_NO = "否"
fileprivate let DEFAULT_TITLE = "提示"

fileprivate let BUTTON_OKAY_COLOR = UIColor.black
fileprivate let BUTTON_CANCEL_COLOR = UIColor.lightGray
fileprivate let BUTTON_YES_COLOR = UIColor.lightGray
fileprivate let BUTTON_NO_COLOR = UIColor.lightGray
fileprivate let DEFAULT_TITLE_COLOR = UIColor.lightGray


class TTAlertView: NSObject {
  
}
//  MARK: - ###### 私有方法 ######
extension TTAlertView {
    class func confirm(title: String?, question: String?, block:@escaping ()->Void) {
        #if false //不兼容iso8.3以前的系统
        let alert = UIAlertController(title: title ?? DEFAULT_TITLE, message: question, preferredStyle: UIAlertControllerStyle.alert)
        let okAction = UIAlertAction(title: BUTTON_OKAY, style: UIAlertActionStyle.default, handler: {(action: UIAlertAction) -> Void in
            block()
        })
        okAction.setValue(BUTTON_OKAY_COLOR, forKey: "titleTextColor")
        alert.addAction(okAction)
        var controller = UIApplication.shared.keyWindow?.rootViewController
        while ((controller?.presentedViewController) != nil) {
            controller = controller?.presentedViewController
        }
        controller?.present(alert, animated: true, completion: nil)
        #else
        if question != nil && question != "" {
            let alertV = YHAlertView(title: title ?? DEFAULT_TITLE, message: question ?? "", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: [BUTTON_OKAY])
            alertV.clickBlock = {(index) in
                block()
            }
            alertV.visual = false
            alertV.animationOption = .none
            alertV.show()
        }
        #endif
    }
    
    class func confirm(title: String?, question: String?) {
        #if false //不兼容iso8.3以前的系统
        let alert = UIAlertController(title: title ?? DEFAULT_TITLE, message: question, preferredStyle: UIAlertControllerStyle.alert)
        let okAction = UIAlertAction(title: BUTTON_OKAY, style: UIAlertActionStyle.default, handler: {(action: UIAlertAction) -> Void in
        })
        okAction.setValue(BUTTON_OKAY_COLOR, forKey: "titleTextColor")
        alert.addAction(okAction)
        var controller = UIApplication.shared.keyWindow?.rootViewController
        while ((controller?.presentedViewController) != nil) {
            controller = controller?.presentedViewController
        }
        controller?.present(alert, animated: true, completion: nil)
        #else
        if question != nil && question != "" {
            let alertV = YHAlertView(title: title ?? DEFAULT_TITLE, message: question ?? "", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: [BUTTON_OKAY])
            alertV.clickBlock = {(index) in
            }
            alertV.visual = false
            alertV.animationOption = .none
            alertV.show()
        }
        
        #endif
    }
    
    class func ask(title: String?, question: String?, okBlock:@escaping ()->Void, noBlock:@escaping ()->Void) {
        #if false //不兼容8.3以前的版本
        let alert = UIAlertController(title: title, message: question, preferredStyle: UIAlertControllerStyle.alert)
        
        let okAction = UIAlertAction(title: BUTTON_OKAY, style: UIAlertActionStyle.default, handler: {(action: UIAlertAction) -> Void in
            okBlock()
        })
        okAction.setValue(BUTTON_OKAY_COLOR, forKey: "titleTextColor")
        let cancelAction = UIAlertAction(title: BUTTON_CANCEL, style: UIAlertActionStyle.cancel, handler: {
            (action: UIAlertAction) -> Void in
            noBlock()
        })
        cancelAction.setValue(BUTTON_CANCEL_COLOR, forKey: "titleTextColor")
        
        alert.addAction(okAction)
        alert.addAction(cancelAction)
        var controller = UIApplication.shared.keyWindow?.rootViewController
        while ((controller?.presentedViewController) != nil) {
            controller = controller?.presentedViewController
        }
        controller?.present(alert, animated: true, completion: nil)
        #else
        if question != nil && question != "" {
            let alertV = YHAlertView(title: title ?? "", message: question ?? "", delegate:nil, cancelButtonTitle: "取消", otherButtonTitles: ["确定"])
            alertV.clickBlock = {(index) in
                if index == 0 {
                    noBlock()
                } else if index == 1 {
                    okBlock()
                }
            }
            alertV.visual = false
            alertV.animationOption = .none
            alertV.show()
        }
        #endif
    }
    
    class func ask(title: String?, question: String?, leftTitle: String?, rightTitle: String?, okBlock:@escaping ()->Void, noBlock:@escaping ()->Void) {
        #if false //不兼容8.3以前的版本
        let alert = UIAlertController(title: title, message: question, preferredStyle: UIAlertControllerStyle.alert)
        
        let okAction = UIAlertAction(title: rightTitle ?? BUTTON_OKAY, style: UIAlertActionStyle.default, handler: {(action: UIAlertAction) -> Void in
            okBlock()
        })
        okAction.setValue(BUTTON_OKAY_COLOR, forKey: "titleTextColor")
        let cancelAction = UIAlertAction(title: leftTitle ?? BUTTON_CANCEL, style: UIAlertActionStyle.cancel, handler: {
            (action: UIAlertAction) -> Void in
            noBlock()
        })
        cancelAction.setValue(BUTTON_CANCEL_COLOR, forKey: "titleTextColor")
        
        alert.addAction(okAction)
        alert.addAction(cancelAction)
        var controller = UIApplication.shared.keyWindow?.rootViewController
        while ((controller?.presentedViewController) != nil) {
            controller = controller?.presentedViewController
        }
        controller?.present(alert, animated: true, completion: nil)
        #else
        if question != nil && question != "" {
            let alertV = YHAlertView(title: title ?? "", message: question ?? "", delegate:nil, cancelButtonTitle: leftTitle ?? BUTTON_CANCEL, otherButtonTitles: [(rightTitle ?? BUTTON_OKAY)])
            alertV.clickBlock = {(index) in
                if index == 0 {
                    noBlock()
                } else if index == 1 {
                    okBlock()
                }
            }
            alertV.visual = false
            alertV.animationOption = .none
            alertV.show()
        }
        #endif
    }
    
    
    class func showMessage(message: String?, second: TimeInterval) {
        #if false
        let alert = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.alert)
        var controller = UIApplication.shared.keyWindow?.rootViewController
        while ((controller?.presentedViewController) != nil) {
            controller = controller?.presentedViewController
        }
        controller?.present(alert, animated: true, completion: {
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + second, execute: {
                var controller = UIApplication.shared.keyWindow?.rootViewController
                while ((controller?.presentedViewController) != nil) {
                    controller = controller?.presentedViewController
                }
                controller?.dismiss(animated: true, completion: nil)
            })
        })
        #else
        if message != nil && message != "" {
            MTProgressHUD.showTips(string: message ?? "", delay: second)
        }
        
        #endif
    }
    
    class func showMessage(message: String?) {
        if message != nil && message != "" {
            showMessage(message: message, second: 1.0)
        }
    }
    
    class func showFromBotton(title: String, infos:[String], completion:@escaping (_ index: NSInteger) -> Void) {
        let alert = UIAlertController(title: title, message: nil, preferredStyle: UIAlertController.Style.actionSheet)
        
        for index in 0..<infos.count {
            
            let action = UIAlertAction(title: infos[index], style: UIAlertAction.Style.default, handler: { (alert) in
                completion(index)
            })
            action.setValue(BUTTON_OKAY_COLOR, forKey: "titleTextColor")
            alert.addAction(action)
        }
        
        
        var controller = UIApplication.shared.keyWindow?.rootViewController
        while ((controller?.presentedViewController) != nil) {
            controller = controller?.presentedViewController
        }
        controller?.present(alert, animated: true, completion: nil)
        
    }
}

